#include "online.h"
#include "ui_online.h"
#include"tcpclient.h"

Online::Online(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Online)
{
    ui->setupUi(this);
    //qDebug()<<"online类中ui的地址为："<<ui;
}

Online::~Online()
{
    delete ui;
}

void Online::showUsr(PDU *pdu)
{
    if(NULL == pdu)
    {
        return;
    }
    //计算在线用户数量，一个用户占用32个字节
    uint uiSize = pdu->uiMsgLen/32;
    char caTemp[32];
    for(uint i=0;i<uiSize;i++)
    {
        //依次读取用户名
        memcpy(caTemp,(char*)(pdu->caMsg)+i*32,32);
        ui->online_lw->addItem(caTemp);
    }
}

void Online::on_addFriend_pb_clicked()
{

    QListWidgetItem *pItem = ui->online_lw->currentItem();
    QString strPerUsrName = pItem->text();
    QString strLoginName = TcpClient::getInstance().loginName();
    PDU *pdu = mkPDU(0);
    pdu->uiMsgType = ENUM_MSG_TYPE_ADD_FRIEND_REQUEST;
    memcpy(pdu->caData,strPerUsrName.toStdString().c_str(),strPerUsrName.size());
    memcpy(pdu->caData+32,strLoginName.toStdString().c_str(),strLoginName.size());
    TcpClient::getInstance().getTcpSocket().write((char*)pdu,pdu->uiPDULen);
    free(pdu);
    pdu = NULL;
}



